/*
 * @Date: 2022-03-22 10:21:23
 * @LastEditTime: 2022-03-25 14:43:21
 */
// pages/personalCenter/login/login.js
import { store } from '../../../store/store';
import {
  createStoreBindings,
  storeBindingsBehavior,
} from 'mobx-miniprogram-bindings';
Page({
  behaviors: [storeBindingsBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    checked: false,
    show: false,
  },
  onChange() {
    this.setData({ checked: !this.data.checked });
  },
  getUserProfile() {
    if (!this.data.checked) {
      wx.showToast({ title: '请先勾选协议', icon: 'none' });
      return;
    }
    wx.getUserProfile({
      desc: '获取你的昵称、头像',
      success: (res) => {
        console.log(res.userInfo);
        this.setData({ show: true });
        let openid = getApp().globalData.openId;
        wx.$cloud.callFunction({
          name:"userModule",  
          data:{
            action:"add_newuser",
            openid:openid,
            nickname:res.userInfo.nickName,
            face:res.userInfo.avatarUrl
          },
          success:res=>{
            wx.setStorage({
              key: "isLogin",
              data: true,
              success: function () {
                  console.log('登录成功')
              },

              fail: function () {
                  console.log('登录失败')
              }

          })
          },
          fail: res=>{
            wx.setStorage({
              key: "isLogin",
              data: false,
          })
          }
      })
    }
  })
  },
  onClose() {
    this.setData({ show: false });
  },
  getPhoneNumber(e) {
    this.onClose();
    console.log(e);
    //store.SET_TOKEN('a132sdf54a6sdf2a1sd34');
    console.log(e)
    var that = this;
     wx.$cloud.callFunction({
       name:"getMobile",
       data: {
         action: "getMobile",
         weRunData: wx.cloud.CloudID(e.detail.cloudID),
       }
     }).then(
       res=>{
        //  console.log(res.result.event.weRunData.data.phoneNumber)
         getApp().globalData.mobile = res.result.event.weRunData.data.phoneNumber;
         that.setData({
           mobile:res.result.event.weRunData.data.phoneNumber,
           isBind:true,
         })}).catch(
           err=>{
             console.error(err);
           }
         )    
    wx.$go2('/pages/techingPositions/index/index', false, 'switchTab');
  },
  go2page(e) {
    this.selectComponent('#navigationBar').go2page(e);
  },

  onLoad: function () {
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: [],
      actions: ['SET_TOKEN'],
    });
    this.selectComponent('#navigationBar').getMenuButtonRect();
  },
});
